import Vue from 'vue'
import Vuex from 'vuex'

Vue.use(Vuex); //vue的插件机制

//Vuex.Store 构造器选项
const Global = new Vuex.Store({
	state: {
		statusBarHeight: 0,
		token: false,
		userInfo: false,
		updateCart: true,
	},
	mutations: {
		init(state) {
			const info = uni.getSystemInfoSync();
			state.statusBarHeight = info.statusBarHeight;
			let token = uni.getStorageSync("token");
			if (token) {
				state.token = token;
			}
			let userInfo = uni.getStorageSync("userInfo");
			if (userInfo) {
				state.userInfo = userInfo;
			}
			let update = uni.getStorageSync('updateCart');
			if(update){
				state.updateCart = update;
			}
		},
		setToken(state, token) {
			state.token = token;
			if (token) {
				uni.setStorage({
					key: "token",
					data: token
				})
			} else {
				uni.removeStorage({
					key: "token",
				})
			}
		},
		setUserInfo(state, userInfo) {
			state.userInfo = userInfo;
			if (userInfo) {
				uni.setStorage({
					key: "userInfo",
					data: userInfo
				})
			} else {
				uni.removeStorage({
					key: "userInfo",
				})
			}
		},
		setCartUpdate(state, updateCart){
			state.updateCart = Boolean(updateCart);
			if(updateCart){
				uni.setStorage({
					key:"updateCart",
					data:updateCart
				})
			}else{
				uni.removeStorage({
					key:"updateCart",
				})
			}
		}
	},
})
export default Global